package com.tianci.appstore.ui.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.view.View;

import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.ui.SkinManager;

public class ProgressBar extends View {
    int width;
    int height;
    private Context context;
    private NinePatchDrawable nineDrawable;
    private Bitmap progressBm;
    private int progress = 0;
    private int range = 100;
    Paint mPaint;
    private boolean showProgressNum = false;

    public ProgressBar(Context context) {
        super(context);
        this.context = context;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        // TODO Auto-generated constructor stub
    }

    public void setProgressDrawable(int resId) {
//         SkinManager.getResDrawable(resId)).getBitmap();
        Drawable d = SkinManager.getResDrawable(resId);
        initBitmapDrawable(d);
    }

    public void setProgressDrawable(Drawable d) {
        initBitmapDrawable(d);
    }

    private void initBitmapDrawable(Drawable d) {
        if (d instanceof BitmapDrawable)
            progressBm = ((BitmapDrawable) d).getBitmap();
        else if (d instanceof NinePatchDrawable)
            nineDrawable = (NinePatchDrawable) d;
    }

    public void setProgress(int progress) {
        LogUtils.i("pro", "setProgress = " + progress);
        if (progress > range)
            return;
        this.progress = progress;
        invalidate();
    }

    public void showProgressNum(float textSize, int color) {
        this.showProgressNum = true;
        mPaint.setTextSize(textSize);
        try {
            if (color < 0)
                mPaint.setColor(color);
            else
                mPaint.setColor(context.getResources().getColor(color));
        } catch (Exception e) {
            mPaint.setColor(Color.WHITE);
        }
    }

    public void hideProgressNum() {
        this.showProgressNum = false;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        int progressW = (int) ((float) progress / (float) range * width);
        Rect destRect = new Rect(0, 0, progressW, height);
        LogUtils.i("pro", "onDraw, width = " + progressW);
        if (progressBm != null) {
            canvas.drawBitmap(progressBm, null, destRect, mPaint);
        } else if (nineDrawable != null) {
            nineDrawable.setBounds(destRect);
            nineDrawable.draw(canvas);
        } else {
            mPaint.setColor(Color.RED);
            canvas.drawRect(destRect, mPaint);
        }
        if (showProgressNum) {
            float textWidth = mPaint.measureText(progress + "%");
            canvas.drawText(progress + "%", (width - textWidth) / 2,
                    (int) ((height + mPaint.getTextSize()) / 2.13), mPaint);
        }
        super.onDraw(canvas);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stub
        this.width = w;
        this.height = h;
        super.onSizeChanged(w, h, oldw, oldh);
    }
}
